package com.wangzhixuan.commons.utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
/** 
* @ClassName: DateUtil 
* @Description: 日期工具类
* @author thomas_liu  
* @date 2018年7月19日 下午10:19:37 
*  
*/
public class DateUtil {
	
	
	public interface DatePattern{
		final String YYYY_MM_DD_HH_MM_SS = "yyyy-MM-dd HH:mm:ss";
		final String YYYYMMDDHHMMSS = "yyyyMMddHHmmss";
	}

	public static final SimpleDateFormat FMT_YYYYMMDDHHMMSS = new SimpleDateFormat(DatePattern.YYYYMMDDHHMMSS);
	public static final SimpleDateFormat FMT_YYYYMMDDHHMMSSS = new SimpleDateFormat("yyyyMMddHHmmssss");
	
	public static final SimpleDateFormat FMT_YYYY_MM_DD_HH_MM_SS = new SimpleDateFormat(DatePattern.YYYY_MM_DD_HH_MM_SS);
	
	public static int differenceDays(Date start, Date end) {
		if (start == null || end == null)
			return 0;
		long t = end.getTime() - start.getTime();
		return (int) (t / 86400000) + ((t % 86400000 == 0) ? 0 : 1);
	}
	/**
	 * 获取指定日期开始时间
	 * @return 
	 */
	public static Date getDateStart(Date date) {
		if(date == null)
			return null;
		Calendar c = Calendar.getInstance();
		c.setTime(date);
		
		c.set(Calendar.HOUR_OF_DAY, 0);
		c.set(Calendar.MINUTE, 0);
		c.set(Calendar.SECOND, 0);
		c.set(Calendar.MILLISECOND, 0);
		
		return c.getTime();
	}
	
	/**
	 * 获取指定日期结束时间
	 * @return 
	 */
	public static Date getDateEnd(Date date) {
		if(date == null)
			return null;
		Calendar c = Calendar.getInstance();
		c.setTime(date);
		
		c.set(Calendar.HOUR_OF_DAY, 23);
		c.set(Calendar.MINUTE, 59);
		c.set(Calendar.SECOND, 59);
		c.set(Calendar.MILLISECOND, 999);
		
		return c.getTime();
	}
	
	/**
	 * 获取当前时间到明天零点的秒数
	 * @return 
	 */
	public static long getTomorrowTime() {
		Calendar c = Calendar.getInstance();
		long current = c.getTimeInMillis();
		
		c.add(Calendar.DAY_OF_YEAR, 1);
		c.set(Calendar.HOUR_OF_DAY, 0);
		c.set(Calendar.MINUTE, 0);
		c.set(Calendar.SECOND, 0);
		c.set(Calendar.MILLISECOND, 0);
		
		return (c.getTimeInMillis() - current) / 1000;
	}
	/**
	 * 获取虚拟电话过期时间
	 * @return
	 */
	public static String getExpiration() {
		Calendar c = Calendar.getInstance();
		c.set(Calendar.SECOND, 120);
		return FMT_YYYY_MM_DD_HH_MM_SS.format(c.getTime());
	}
	
	public static Date parse_yyyyMMddHHmmss(String source){
		if(source == null)
			return null;
		try {
			return FMT_YYYYMMDDHHMMSS.parse(source);
		} catch (ParseException e) {
		}
		return null;
	}
	
	public static String format_yyyyMMddHHmmss(Date source){
		if(source == null)
			return null;
		return FMT_YYYYMMDDHHMMSS.format(source);
	}
	
	/**
	 * 
	 * @Decription: 获取当前系统时间
	 * @return
	 */
	public static String getSystemTime(){
		return String.valueOf(System.currentTimeMillis());
	}
	
	public static String getTimeStamp() {
		int time = (int) (System.currentTimeMillis() / 1000);
		return String.valueOf(time);
	}
	
	public static void main(String[] args) {
		System.out.println(getTomorrowTime());
	}

}